# clean environment
rm(list=ls())

# load packages
library(ggplot2)
library(openxlsx)

# load data set with chapter lengths
dat = openxlsx::read.xlsx("vsberichte_metadata.xlsx", sheet = 1)

# binary varibable indicating whether interior minister is from a center-right party
dat$cr_intmin <- ifelse(dat$intmin_party == "cdu" | dat$intmin_party== "csu" | dat$intmin_party=="fdp", 1, ifelse(dat$intmin_party=="pro", NA, 0))

# create chapter length variable by taking the difference between the page number in which the RWE and LWE chapter ends and begins
dat$rwe_chapter_length <- dat$page_right_end-dat$page_right_start
dat$lwe_chapter_length <- dat$page_left_end-dat$page_left_start

# sum up chapter lengths on the year level, by interior minister partisanship 
dat_agg <- aggregate(cbind(rwe_chapter_length, lwe_chapter_length)~year+cr_intmin, dat, sum)

# create logged ratio of RWE and LWE chapter lengths
dat_agg$rwe_lwe_chapter_ratio_log <- log(dat_agg$rwe_chapter_length + 0.5) - log(dat_agg$lwe_chapter_length + 0.5)

# plot logged ratios over time
ggplot(data=dat_agg, aes(x=year, y=rwe_lwe_chapter_ratio_log, 
                         color=factor(cr_intmin), 
                         fill=factor(cr_intmin),
                         shape=factor(cr_intmin))) + 
  geom_point(size=2.5) + 
  #  stat_smooth() +
  geom_line() +
  geom_hline(yintercept=0, linetype="dashed") +
  theme_classic() +
  theme(legend.position = "bottom", text=element_text(size=16)) +
  scale_color_manual(values=c("firebrick", "dodgerblue"), name="Interior Minister Party", labels=c("Center-Left", "Center-Right")) +
  scale_fill_manual(values=c("firebrick", "dodgerblue"), name="Interior Minister Party", labels=c("Center-Left", "Center-Right")) +
  scale_shape_manual(values=c(16, 17), name="Interior Minister Party", labels=c("Center-Left", "Center-Right")) +
  ylab("Logged Ratio of Chapter Length") + xlab("") +
  scale_x_continuous(breaks=seq(1970, 2025, 10))

ggsave("Fig4_1.pdf", width=7, height=5)

